//#include "tcpsession.h"
//#include "mainwindow.h"
//#include "ui_mainwindow.h"
//tcpSession::tcpSession()
//{

//}


//void tcpSession::tcpInit()
//{
//    mTcpSocket = new QTcpSocket(this); // 在这里使用this作为父对象，确保在窗口关闭时自动释放资源
//    QString addr = ui->lineEdit_10->text();
//    qint16 port = ui->lineEdit_11->text().toInt();

//    connect(mTcpSocket, &tcpSession::connected, this, &tcpSession::onConnected);
//    connect(mTcpSocket, &tcpSession::disconnected, this, &tcpSession::onDisConnected);
//    connect(mTcpSocket, &tcpSession::stateChanged, this, &tcpSession::onStateChanged);
//    connect(mTcpSocket, &tcpSession::readyRead, this, &tcpSession::onReadyRead);
//    connect(mTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));

//    mTcpSocket->connectToHost(addr, port);
//}


//void tcpSession::tcpWrite(QByteArray sMsg)
//{
//    if(mTcpSocket == nullptr)
//    {
//        ui->textEdit_3->append(ui_codec->toUnicode("没有连接设备"));
//        return;
//    }
//    QByteArray msg;
//    msg = sMsg;
//    uint16_t crc = sendModbusCRC16(msg);
//    msg.push_back(crc & 0xff);
//    msg.push_back((crc >> 8)&0xff);

//    mTcpSocket->write(msg);
//}

//void tcpSession::tcpDisconnect()
//{
//    if(mTcpSocket->state() == QAbstractSocket::ConnectedState)
//    {
//        mTcpSocket->disconnectFromHost();
//    }
//}

//void tcpSession::onConnected()
//{
//    ui->textEdit_3->append(ui_codec->toUnicode("已连接到服务器)"));
//    ui->textEdit_3->append(ui_codec->toUnicode("设备IP: ")+mTcpSocket->peerAddress().toString());
//    ui->textEdit_3->append(ui_codec->toUnicode("设备端口: ")+QString::number(mTcpSocket->peerPort()));
//}

//void tcpSession::onDisConnected()
//{
//    //ui->textEdit_3->append(ui_codec->toUnicode("已断开服务器"));
//}

//void tcpSession::onStateChanged(QAbstractSocket::SocketState)
//{

//}

//void tcpSession::displayError(QAbstractSocket::SocketError socketError)
//{
//    switch (socketError) {
//    case QAbstractSocket::RemoteHostClosedError:
//        ui->textEdit_3->append(ui_codec->toUnicode("远程主机关闭了连接"));
//        break;
//    case QAbstractSocket::HostNotFoundError:
//        ui->textEdit_3->append(ui_codec->toUnicode("找不到主机"));
//        break;
//    case QAbstractSocket::ConnectionRefusedError:
//        ui->textEdit_3->append(ui_codec->toUnicode("该连接被对等方拒绝"));
//        break;
//    default:
//        ui->textEdit_3->append(ui_codec->toUnicode("出现以下错误: ") + mTcpSocket->errorString());
//    }
//}

//void tcpSession::onReadyRead()
//{
//    QByteArray msg = mTcpSocket->readAll();
//    tcp_queue.enqueue(msg);
//}
